package com.efounder.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.efounder.chat.activity.BaseActivity;
import com.efounder.mobilemall.R;
import com.efounder.utils.MainConstant;
import com.efounder.utils.ResStringUtil;
import com.efounder.view.CancelOrOkDialog;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
/**
 * Created by wei on 2018/3/08/02.
 * 上传评论的大图展示
 */

public class PlusImageActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {

    ViewPager viewPager; //展示图片的ViewPager
    TextView positionTv; //图片的位置，第几张图片
    ImageView iv_back;
    TextView tv_save;

    private ArrayList<String> imgList; //图片的数据源
    private int mPosition; //第几张图片
    private ViewPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plus_image);

        imgList = getIntent().getStringArrayListExtra(MainConstant.IMG_LIST);
        mPosition = getIntent().getIntExtra(MainConstant.POSITION, 0);
        initView();
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        positionTv = (TextView) findViewById(R.id.tv_title);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_save = (TextView) findViewById(R.id.tv_save);
        iv_back.setOnClickListener(this);
        tv_save.setText(R.string.common_text_delete);
        tv_save.setVisibility(View.VISIBLE);
        tv_save.setOnClickListener(this);
        viewPager.addOnPageChangeListener(this);

        mAdapter = new ViewPagerAdapter(this, imgList);
        viewPager.setAdapter(mAdapter);
        positionTv.setText(mPosition + 1 + "/" + imgList.size());
        viewPager.setCurrentItem(mPosition);
    }

    //删除图片
    private void deletePic() {
        if (imgList.size() > 0) {
            CancelOrOkDialog dialog = new CancelOrOkDialog(this, ResStringUtil.getString(R.string.mail_want_delete_photo)) {
                @Override
                public void ok() {
                    super.ok();
                    imgList.remove(mPosition); //从数据源移除删除的图片
                    setPosition();
                    dismiss();
                }
            };
            dialog.show();
        }else {
            CancelOrOkDialog dialog = new CancelOrOkDialog(this, ResStringUtil.getString(R.string.mail_no_have_photo)) {
                @Override
                public void ok() {
                    super.ok();
                   back();
                }
            };
            dialog.show();
        }

    }

    //设置当前位置
    private void setPosition() {
        if (imgList.size() == 0) {
            positionTv.setText(mPosition + 0 + "/" + imgList.size());
        } else {
            positionTv.setText(mPosition + 1 + "/" + imgList.size());
        }
        viewPager.setCurrentItem(mPosition);
        mAdapter.notifyDataSetChanged();
    }

    //返回上一个页面
    private void back() {
        Intent intent = getIntent();
        intent.putStringArrayListExtra(MainConstant.IMG_LIST, imgList);
        setResult(MainConstant.RESULT_CODE_VIEW_IMG, intent);
        finish();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mPosition = position;
        positionTv.setText(position + 1 + "/" + imgList.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.iv_back) {//返回
            back();

        } else if (i == R.id.tv_save) {//删除图片
            deletePic();

        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //按下了返回键
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    public class ViewPagerAdapter extends PagerAdapter {

        private Context context;
        private List<String> imgList; //图片的数据源

        public ViewPagerAdapter(Context context, List<String> imgList) {
            this.context = context;
            this.imgList = imgList;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public int getCount() {
            return imgList.size();
        }

        //判断当前的View 和 我们想要的Object(值为View) 是否一样;返回 true/false
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }

        //instantiateItem()：将当前view添加到ViewGroup中，并返回当前View
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View itemView = getItemView(R.layout.view_pager_img);
            PhotoView imageView = (PhotoView) itemView.findViewById(R.id.img_iv);
            Glide.with(context).load(imgList.get(position)).into(imageView);
            container.addView(itemView);
            return itemView;
        }

        //destroyItem()：删除当前的View;
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        private View getItemView(int layoutId) {
            View itemView = LayoutInflater.from(this.context).inflate(layoutId, null, false);
            return itemView;
        }
    }
}
